Expand description
Easily registered distributed metrics.
More docs todo…
Creating a Metric
Registering a metric is straightforward. All that’s needed is to declare a
static within the metric
macro. By default, the metric will have the
name of the path to the static variable you used to declare it but this can
be overridden by passing the name
parameter to the macro.
use metriken::*;
/// A counter metric named "<crate name>::COUNTER_A"
#[metric]
static COUNTER_A: Counter = Counter::new();
/// A counter metric named "my.metric.name"
#[metric(name = "my.metric.name")]
static COUNTER_B: Counter = Counter::new();
Accessing Metrics
All metrics registered via the metric
macro can be accessed by calling
the metrics()
function. This will return an instance of the Metric
struct which allows you to access all staticly and dynamically registered
metrics.
Suppose we have the metrics declared in the example above.
let metrics = metrics();
// Metrics may be in any arbitrary order
let mut names: Vec<_> = metrics.iter().map(|metric| metric.name()).collect();
names.sort();
assert_eq!(names.len(), 2);
assert_eq!(names[0], "COUNTER_A");
assert_eq!(names[1], "my.metric.name");
How it Works
Behind the scenes, this crate uses the [linkme
] crate to create a
distributed slice containing a MetricEntry
instance for each metric that
is registered via the metric
attribute.
Re-exports
pub use crate::dynmetrics::DynBoxedMetric;
pub use crate::dynmetrics::DynPinnedMetric;
pub use crate::dynmetrics::MetricBuilder;
pub use crate::histogram::AtomicHistogram;
pub use crate::histogram::RwLockHistogram;
Modules
- Methods and structs for working with dynamically created and destroyed metrics.
Structs
- A counter. Can be incremented or added to.
- An iterator over all dynamically registered metrics.
- A gauge. Indicates the current value of some host parameter.
- A value which is initialized on the first access.
- Metadata for a metric.
- An iterator over the entries of a
Metadata
. - A statically declared metric entry.
- Provides access to all registered metrics both static and dynamic.
- An iterator over all registered metrics.
Enums
- The value of a metric.
Traits
- Global interface to a metric.
Functions
- The default formatter supports Prometheus-style exposition, and otherwise simply prints the metric name.
- The list of all metrics registered via the either
metric
attribute or by using the types within thedynmetrics
module.
Type Aliases
- A counter holds a unsigned 64bit monotonically non-decreasing value. The counter behavior is to wrap on overflow.
- A gauge holds a signed 64-bit value and is used to represent metrics which may increase or decrease in value. The behavior is to wrap around on overflow and underflow.
Attribute Macros
- Declare a global metric that can be accessed via the
metrics
method.